package com.innazis.jfrogsurvey.backendmanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.innazis.jfrogsurvey.config.Constants;
import com.innazis.jfrogsurvey.obj.SurveyObj;

public class ModelManager {
	private static String TAG = "ModelManager";

	static ModelManager instance;
	private static RequestQueue mVolleyQueue;

	public static ModelManager getInstance(Activity activity) {
		if (instance == null) {
			instance = new ModelManager();
			mVolleyQueue = Volley.newRequestQueue(activity);
		}
		return instance;
	}

	public void sendData(final Activity activity, List<SurveyObj> AllSurveys,
			String senderID, final ModelManagerListener listener) {

		JSONObject paramsObj = ParameterUtility.CreateSendDataParamsJSON(
				AllSurveys, senderID);
		Log.d("SendData", "Params :" + paramsObj.toString());
		JsonObjectRequest jsonObjRequest = new JsonObjectRequest(
				Request.Method.POST, Constants.URL_SEND_DATA, paramsObj,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						listener.onSuccess(response);
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						if (error instanceof NetworkError) {
						} else if (error instanceof ServerError) {
						} else if (error instanceof AuthFailureError) {
						} else if (error instanceof ParseError) {
						} else if (error instanceof NoConnectionError) {
						} else if (error instanceof TimeoutError) {
						}
						listener.onError(error.getLocalizedMessage());
					}
				});

		// Set a retry policy in case of SocketTimeout & ConnectionTimeout
		// Exceptions. Volley does retry for you if you have specified the
		// policy.
		jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
				DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
				DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		jsonObjRequest.setTag("SEND_DATA_TAG");
		mVolleyQueue.add(jsonObjRequest);
	}

	public void updateSurvey(final Activity activity,
			final ModelManagerListener listener) {
		JsonObjectRequest jsonObjRequest = new JsonObjectRequest(
				Request.Method.POST, Constants.URL_UPDATE_TABLES, null,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						listener.onSuccess(response);
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						if (error instanceof NetworkError) {
						} else if (error instanceof ServerError) {
						} else if (error instanceof AuthFailureError) {
						} else if (error instanceof ParseError) {
						} else if (error instanceof NoConnectionError) {
						} else if (error instanceof TimeoutError) {
						}
						listener.onError(error.getLocalizedMessage());
					}
				});

		// Set a retry policy in case of SocketTimeout & ConnectionTimeout
		// Exceptions. Volley does retry for you if you have specified the
		// policy.
		jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
				DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
				DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		jsonObjRequest.setTag("INIT_TAG");
		mVolleyQueue.add(jsonObjRequest);
	}

	private String buildGetParams(String baseUrl, HashMap<String, String> params) {
		StringBuffer strBuffer = new StringBuffer(baseUrl);
		strBuffer.append("?");
		for (Entry<String, String> item : params.entrySet()) {
			strBuffer.append(item.getKey()).append("=").append(item.getValue())
					.append("&");
		}
		return strBuffer.toString().substring(0,
				strBuffer.toString().length() - 1);
	}

}
